import random
import requests
from fake_useragent import UserAgent

class AntiSpiderMiddleware:
    def __init__(self):
        self.ua = UserAgent()
        self.proxy_pool = [
            'http://proxy1.example.com:8080',
            'http://proxy2.example.com:8080'
        ]
        
    def get_random_proxy(self):
        return random.choice(self.proxy_pool)
    
    def safe_request(self, url):
        headers = {
            'User-Agent': self.ua.random,
            'Referer': 'https://www.csdn.net/'
        }
        proxies = {'http': self.get_random_proxy()}
        response = requests.get(url, headers=headers, proxies=proxies, timeout=10)
        response.encoding = 'utf-8'
        return response.text